
//////////// translation class , loaded first
var langUtil = {
	getCurrLang: function(){
		var currlang = Cookie.get('LANG',{path: wf_cookie_path});
		if(!currlang){
			currlang = defaultLang;
		}
		return currlang;
	},
    preloadL10NModuleByArrWidget: function(arrWidget){
    	var onSuccessGetL10N = function(response){
    		var jsonL10N = JSON.decode(response);
    		for(var i=0,j=arrWidget.length;i<j;i++){
        		var strWidgetName = arrWidget[i];
	    		if(window[strWidgetName+'_locLang'] == undefined) {
	            	if(jsonL10N[strWidgetName] != undefined){ //set L10N by jsonL10N[strWidgetName]
	            		window[strWidgetName+'_locLang'] = new localLang(strWidgetName, jsonL10N[strWidgetName]);
	            	}else{ //sent an ajax to get L10N
	                    window[strWidgetName+'_locLang'] = new localLang(strWidgetName);
	            	}
	            }
    		}
    	}
        
    	var arrWidgetHaveToGetL10N = [];
    	for(var i=0,j=arrWidget.length;i<j;i++){
    		var strWidgetName = arrWidget[i];
    		if(window[strWidgetName+'_locLang'] == undefined){
        		arrWidgetHaveToGetL10N[arrWidgetHaveToGetL10N.length] = strWidgetName;
        	}
    	}
        if(arrWidgetHaveToGetL10N.length > 0){
            var myRequest = new Request({url: wfc_servercgi_other_controller, method: 'post', onSuccess: onSuccessGetL10N, async: false});
			myRequest.send({data:'act=getJsonLangFileByCsvFolder&lang='+langUtil.getCurrLang()+'&folder='+arrWidgetHaveToGetL10N.join(',')+'&langType='+localLangUtil.getLangType()+'&userGenerated='+localLangUtil.getUserGeneratedInfo()});
        }
    },
    preloadL10NProductByArrBaseWidget: function(arrBaseWidget){
    	var onSuccessGetL10N = function(response){
    		var jsonL10N = JSON.decode(response);
    		for(var i=0,j=arrBaseWidget.length;i<j;i++){
        		var strBaseWidgetName = arrBaseWidget[i];
	    		if(window[strBaseWidgetName+'_proLang'] == undefined) {
	            	if(jsonL10N[strBaseWidgetName] != undefined){ //set L10N by jsonL10N[strBaseWidgetName]
	            		window[strBaseWidgetName+'_proLang'] = new ProductLang(strBaseWidgetName, jsonL10N[strBaseWidgetName]);
	            	}else{ //sent an ajax to get L10N
	                    window[strBaseWidgetName+'_proLang'] = new ProductLang(strBaseWidgetName);
	            	}
	            }
    		}
    	}
        
    	var arrBaseWidgetHaveToGetL10N = [];
    	for(var i=0,j=arrBaseWidget.length;i<j;i++){
    		var strBaseWidgetName = arrBaseWidget[i];
    		if(window[strBaseWidgetName+'_proLang'] == undefined){
        		arrBaseWidgetHaveToGetL10N[arrBaseWidgetHaveToGetL10N.length] = strBaseWidgetName;
        	}
    	}
        if(arrBaseWidgetHaveToGetL10N.length > 0){
            var myRequest = new Request({url: wfc_servercgi_other_controller, method: 'post', onSuccess: onSuccessGetL10N, async: false});
			myRequest.send({data:'act=getJsonLangFileByCsvFolder&lang='+langUtil.getCurrLang()+'&folder='+arrBaseWidgetHaveToGetL10N.join(',')+'&langType='+productLangUtil.getLangType()+'&userGenerated='+productLangUtil.getUserGeneratedInfo()});
        }
    },
    preloadL10NWidgetComponentByArrWidgetComponent: function(arrWidgetComponent){
    	var onSuccessGetL10N = function(response){
    		var jsonL10N = JSON.decode(response);
    		for(var i=0,j=arrWidgetComponent.length;i<j;i++){
        		var strWidgetComponentName = arrWidgetComponent[i];
	    		if(window[strWidgetComponentName+'_comLang'] == undefined) {
	            	if(jsonL10N[strWidgetComponentName] != undefined){ //set L10N by jsonL10N[strWidgetComponentName]
	            		window[strWidgetComponentName+'_comLang'] = new comLang(strWidgetComponentName, jsonL10N[strWidgetComponentName]);
	            	}else{ //sent an ajax to get L10N
	                    window[strWidgetComponentName+'_comLang'] = new comLang(strWidgetComponentName);
	            	}
	            }
    		}
    	}
        
    	var arrWidgetComponentHaveToGetL10N = [];
    	for(var i=0,j=arrWidgetComponent.length;i<j;i++){
    		var strWidgetComponentName = arrWidgetComponent[i];
    		if(window[strWidgetComponentName+'_comLang'] == undefined){
        		arrWidgetComponentHaveToGetL10N[arrWidgetComponentHaveToGetL10N.length] = strWidgetComponentName;
        	}
    	}
        if(arrWidgetComponentHaveToGetL10N.length > 0){
            var myRequest = new Request({url: wfc_servercgi_other_controller, method: 'post', onSuccess: onSuccessGetL10N, async: false});
			myRequest.send({data:'act=getJsonLangFileByCsvFolder&lang='+langUtil.getCurrLang()+'&folder='+arrWidgetComponentHaveToGetL10N.join(',')+'&langType='+comLangUtil.getLangType()+'&userGenerated='+comLangUtil.getUserGeneratedInfo()});
        }
    }
};
var langsBase = new Class({
		currlang: "en_US",
		defaultlanguage: defaultLang,
		labellist : null,
		
		// ---- intiailize the object and load the language
		initialize: function(){
			// initial this.currlang && this.labellist
			this.initialProperty();

			var myRequest = new Request({url: wfc_servercgi_other_controller, method: 'post', onSuccess: this.onSuccess.bind(this), async: false});
            /**
                            *  lang: language, ex. en_US
                            * folder: different language type has its own folder,ex. PRODUCT_LANG has its own folder - modBaseWidget
                            * langType: three kinds of languages,ex. WF_LANG, PRODUCT_LANG, WIDGET_LANG
                            * userGenerated: whether widget is user-generated
                            */
			myRequest.send({data:'act=getLangFile&lang='+this.currlang+'&folder='+this.getFolder()+'&langType='+this.getLangType()+'&userGenerated='+this.getUserGeneratedInfo()});
		},
		initialProperty: function(){
			this.currlang = langUtil.getCurrLang();

			this.labellist = new Hash();
		},
		initializeByJsonL10N: function(jsonL10N){
			// initial this.currlang && this.labellist
			this.initialProperty();
			for(var key in jsonL10N){
				if (jsonL10N.hasOwnProperty(key)){
					//alert(key + " -> " + p[key]);
					this.labellist.set([key] , (jsonL10N[key] ? jsonL10N[key] : ""));
				}
			}
		},
		initializeByObjLang: function(objLang){
			// initial this.currlang && this.labellist
			this.initialProperty();
			this.labellist = objLang.labellist;
		},
		onSuccess: function(responseText, responseXML)
		{        
			if (responseXML)
			{
				var items = responseXML.getElementsByTagName("string");
				for(var i=0; i<items.length; i++)
				{
					this.labellist.set([items[i].getAttribute("id")] , (items[i].firstChild ? items[i].firstChild.nodeValue : ""));
				}
			}
		},
        getLang: function(){
            var lang = this.currlang;
            if(wfc_mapping_lang === true){
                var request = new Request({
                    async: false,
                    url: wfc_servercgi_other_controller+'?'+ AddUrlParam(), 
                    method: 'post', 
                    data: 'act=getLang&lang='+this.currlang,
                    onSuccess: function(response){
                        lang = response;
                        wfc_mapping_lang = response;
                    }
                }).send();
            }
            else if(wfc_mapping_lang !== false){
                lang = wfc_mapping_lang;
            }
            return lang;      
        },
        getFolder: function(){
        },

        getLangType: function(){
        },

        getUserGeneratedInfo: function(){
        },
		
        // find the label value ,if not found , return the string
        label: function(word, arrParam){
            return htmlentities(this.labelText(word, arrParam));
        },
        labelText: function(word, arrParam){
            // loading has been complete
            var trans = this.labellist.get(word);
            if(trans == null) trans = word;
            if(arrParam){
                arrParam.each(function(param,i){
                    var regexp = new RegExp('\\{'+i+'\\}', 'g');
                    trans = trans.replace(regexp, param);
                });
            }
            return trans;
        },
		
		/// change language , set in cookie and reload
		changeLang: function(lang){
			if(ud == "ok"){
				Cookie.set("LANG", lang,{path: wf_cookie_path});
				sendData(dbHandler, JSON.encode({act:'lang', lang:lang}), refreshPage);
			}else{
				Cookie.set("LANG", lang,{path: wf_cookie_path});
				refreshPage();
			}
		}
});

/**
 *	\brief	load framework language resource
*/
var langs = new Class({
	Extends: langsBase,

    getFolder: function(){
        return "";
    },

    getLangType: function(){
        return "WF_LANG";
    },

    getUserGeneratedInfo: function(){
        return 0;
    }
});

/**
 *	\brief	load product specific language resource
*/
var productLangUtil = {
	getLangType: function(){
		return "PRODUCT_LANG";
	},
	getUserGeneratedInfo: function(){
		return 0;
    }
};
var ProductLang = new Class({
	Extends: langsBase,
	ProductName: null,

	/**
	 *	\brief	intiailize the object and load the language
	 *
	 *	\param[in]	ProductName
	*/
	initialize: function(ProductName, jsonL10N){
		this.ProductName = ProductName;
		if(jsonL10N != undefined){
			this.initializeByJsonL10N(jsonL10N);
		}else{ //sent ajax to get L10N
			if(window[ProductName+'_proLang'] == undefined){
				this.parent();
			}else{
				this.initializeByObjLang(window[ProductName+'_proLang']);
			}
		}	
	},

    getFolder: function(){
        return this.ProductName;
    },

    getLangType: function(){
        return productLangUtil.getLangType();
    },

    getUserGeneratedInfo: function(){
        return productLangUtil.getUserGeneratedInfo();
    }
});


/**
 *	\brief	load widget specific language resource
*/
var localLangUtil = {
	getLangType: function(){
		return "WIDGET_LANG";
	},
	getUserGeneratedInfo: function(){
        if (g_arrUserGeneratedInfoOfWidget[this.modName] == 1){
            return 1;
        }else{
            return 0;
        }
    }
};
var localLang = new Class({
	Extends: langsBase,
	modName: null,

	/**
	 *	\brief	intiailize the object and load the language
	 *
	 *	\param[in]	modName		Widget module name
	*/
	initialize: function(modName, jsonL10N){
		this.modName = modName;
		if(jsonL10N != undefined){
			this.initializeByJsonL10N(jsonL10N);
		}else{ 
			if(window[modName+'_locLang'] == undefined){ //sent ajax to get L10N
				this.parent();
			}else{
				this.initializeByObjLang(window[modName+'_locLang']);
			}
		}
	},

    getFolder: function(){
        return this.modName;
    },

    getLangType: function(){
        return localLangUtil.getLangType();
    },

    getUserGeneratedInfo: function(){
        return localLangUtil.getUserGeneratedInfo();
    }
});


/**
 *	\brief	load component specific language resource
*/
var comLangUtil = {
	getLangType: function(){
		return "COMPONENT_LANG";
	},
	getUserGeneratedInfo: function(){
		return 0;
    }
};
var comLang = new Class({
	Extends: langsBase,
	comName: null,

	/**
	 *	\brief	intiailize the object and load the language
	 *
	 *	\param[in]	comName		Widget module name
	*/
	initialize: function(comName, jsonL10N){
		this.comName = comName;
		if(jsonL10N != undefined){
			this.initializeByJsonL10N(jsonL10N);
		}else{ 
			if(window[comName+'_comLang'] == undefined){ //sent ajax to get L10N
				this.parent();
			}else{
				this.initializeByObjLang(window[comName+'_comLang']);
			}
		}
	},

    getFolder: function(){
        return this.comName;
    },

    getLangType: function(){
        return comLangUtil.getLangType();
    },

    getUserGeneratedInfo: function(){
        return comLangUtil.getUserGeneratedInfo();
    }
});

/**
 *	\brief	load new ProductLang specific language resource
*/
var NewProductLang = new Class({
    Extends: langsBase,
    ProductName: null,

    /**
     *	\brief	intiailize the object and load the language
     *
     *	\param[in]	ProductName
    */
    initialize: function(ProductName){
        this.ProductName = ProductName;
        this.parent();
    },

    getFolder: function(){
        return this.ProductName;
    },

    getLangType: function(){
        return "NEW_PRODUCT_LANG";
    },

    getUserGeneratedInfo: function(){
        return 0;
    }
});